package com.itestor.iptv.global.utils;

import com.itestor.iptv.modules.common.vo.User;
import net.sf.navigator.menu.MenuComponent;
import net.sf.navigator.menu.PermissionsAdapter;

/**
 * struts-menu的权限相关的控制器,根据用户权限来控制菜单可见-不可见.
 * @author Mr.li
 */
public class MenuPermissionAdapter implements PermissionsAdapter {

    /**
     * 当前登陆的用户.
     */
    private final User user;

    /**
     * Creates a new instance of MenuPermissionAdapter.
     * @param pUser 当前登陆用户
     */
    public MenuPermissionAdapter(User user) {
        super();
        this.user = user;
    }

    /**
     * 确定该菜单是否是当前用户可以访问的菜单.
     * @param menu 菜单
     * @return boolean
     */
    public boolean isAllowed(MenuComponent menu) {
        if (menu.getRoles() == null) {
            return true;
        } else {
            String[] allowedRoles = menu.getRoles().split(",");
            for (String allowedRole : allowedRoles) {
                if (allowedRole.equals(user.getRolename())) {
                    return true;
                }
            }
        }
        return false;
    }

}
